﻿namespace PugLib.Extensions
{
	using System;
	using System.Globalization;

	public static class Basic
	{
		public static void ToConsoleLine(this object input)
		{
			Console.WriteLine(input);
		}

		[CLSCompliant(false)]
		public static T To<T>(this IConvertible s)
		{
			return (T)Convert.ChangeType(s, typeof(T), CultureInfo.InvariantCulture);
		}


		////public static void Require<T>(this T obj, Expression<Func<T, bool>> pred)
		////{
		////    Func<T, bool> x = pred.Compile();
		////    if (!x(obj))
		////    {
		////        throw new InvalidOperationException(pred.ToString());
		////    }
		////}

		//public static void Require<T>(this T obj, Func<T, bool> pred)
		//{
		//    if (!pred(obj))
		//    {
		//        throw new InvalidOperationException(pred.ToString());
		//    }
		//}

		/// <summary>
		/// Attempts to parse the provided version string in to a Version instance.
		/// </summary>
		/// <param name="version">ex: "1.2.2"</param>
		/// <param name="value">parsed Version instance</param>
		/// <returns>true if parse succeeds; version is valid. false if the version string is invalid</returns>
		public static bool TryParseVersion(this string version, out Version value)
		{
			try
			{
				value = new Version(version);
				return true;
			}
			catch (ArgumentException)
			{
				value = null;
			}
			catch (FormatException)
			{
				value = null;
			}
			return false;
		}

		#region null

		public static bool IsNull<T>(this T input)
			where T : class
		{
			return (input == null) || (input == DBNull.Value);
		}

		#endregion null
	}
}